#!/usr/bin/python3

from log import *
from mysqlDB import *
import os
import sys
import time
import datetime



tables=[
"t_account",
"t_acl",
"t_action",
"t_activity",
"t_api",
"t_apispec",
"t_apistruct",
"t_apistruct_spec",
"t_api_lib_release",
"t_asset",
"t_assetlib",
"t_attend",
"t_attendstat",
"t_auditcl",
"t_auditplan",
"t_auditresult",
"t_baseimage",
"t_baseimagebrowser",
"t_basicmeas",
"t_block",
"t_branch",
"t_browser",
"t_budget",
"t_bug",
"t_build",
"t_burn",
"zt_case",
"zt_casestep",
"zt_cmcl",
"zt_company",
"zt_compile",
"zt_config",
"zt_cron",
"zt_deploy",
"zt_deployproduct",
"zt_deployscope",
"zt_deploystep",
"zt_dept",
"zt_design",
"zt_designspec",
"zt_doc",
"zt_doccontent",
"zt_doclib",
"zt_domain",
"zt_durationestimation",
"zt_effort",
"zt_entry",
"zt_expect",
"zt_extension",
"zt_faq",
"zt_feedback",
"zt_feedbackview",
"zt_file",
"zt_gapanalysis",
"zt_group",
"zt_grouppriv",
"zt_history",
"zt_holiday",
"zt_host",
"zt_im_chat",
"zt_im_chatuser",
"zt_im_chat_message_index",
"zt_im_client",
"zt_im_conference",
"zt_im_conferenceaction",
"zt_im_message",
"zt_im_messagestatus",
"zt_im_message_backup",
"zt_im_message_index",
"zt_im_queue",
"zt_im_userdevice",
"zt_intervention",
"zt_issue",
"zt_job",
"zt_kanban",
"zt_kanbancard",
"zt_kanbancell",
"zt_kanbancolumn",
"zt_kanbangroup",
"zt_kanbanlane",
"zt_kanbanregion",
"zt_kanbanspace",
"zt_lang",
"zt_leave",
"zt_lieu",
"zt_log",
"zt_measqueue",
"zt_measrecords",
"zt_meastemplate",
"zt_meeting",
"zt_meetingroom",
"zt_module",
"zt_mr",
"zt_mrapproval",
"zt_nc",
"zt_notify",
"zt_oauth",
"zt_object",
"zt_opportunity",
"zt_overtime",
"zt_pipeline",
"zt_planstory",
"zt_process",
"zt_product",
"zt_productplan",
"zt_programactivity",
"zt_programoutput",
"zt_programprocess",
"zt_programreport",
"zt_project",
"zt_projectcase",
"zt_projectproduct",
"zt_projectspec",
"zt_projectstory",
"zt_relation",
"zt_relationoftasks",
"zt_release",
"zt_repo",
"zt_repobranch",
"zt_repofiles",
"zt_repohistory",
"zt_report",
"zt_researchplan",
"zt_researchreport",
"zt_review",
"zt_reviewcl",
"zt_reviewissue",
"zt_reviewlist",
"zt_reviewresult",
"zt_risk",
"zt_score",
"zt_searchdict",
"zt_searchindex",
"zt_serverroom",
"zt_service",
"zt_solutions",
"zt_sqlview",
"zt_stage",
"zt_stakeholder",
"zt_story",
"zt_storyestimate",
"zt_storyreview",
"zt_storyspec",
"zt_storystage",
"zt_suitecase",
"zt_task",
"zt_taskestimate",
"zt_taskspec",
"zt_team",
"zt_testreport",
"zt_testresult",
"zt_testrun",
"zt_testsuite",
"zt_testtask",
"zt_todo",
"zt_traincategory",
"zt_traincontents",
"zt_traincourse",
"zt_trainplan",
"zt_trainrecords",
"zt_trip",
"zt_user",
"zt_usercontact",
"zt_usergroup",
"zt_userquery",
"zt_usertpl",
"zt_userview",
"zt_vm",
"zt_vmtemplate",
"zt_webhook",
"zt_weeklyreport",
"zt_workestimation",
"zt_workflow",
"zt_workflowaction",
"zt_workflowdatasource",
"zt_workflowfield",
"zt_workflowlabel",
"zt_workflowlayout",
"zt_workflowlinkdata",
"zt_workflowrelation",
"zt_workflowrelationlayout",
"zt_workflowreport",
"zt_workflowrule",
"zt_workflowsql",
"zt_workflowversion",
"zt_zoutput"]

today = datetime.datetime.now().strftime('%Y%m%d')  
mylog=CLog(logName="zentao_test_support_data_sync" + today, isPrint=True)
zentaoTestDB= CMySqlDB(log=mylog, dbHost="10.1.52.204", dbUser="shengli", dbPwd="shengli0", dbName="zentao")
zentaoSupprotDB= CMySqlDB(log=mylog, dbHost="10.1.52.203", dbUser="shengli", dbPwd="shengli0", dbName="zentao")

def dataSync():
    while true:
        for table in tables:
            zentaoTestDB.query("select * from " + table)
            zentaoTestData = zentaoTestDB.getData()
            zentaoSupprotDB.query("select * from " + table)
            zentaoSupportData = zentaoSupprotDB.getData()
            # 数量量相等
            if (len(zentaoSupportData) == len(zentaoSupportData)):
                for index in range(0, len(zentaoTestData)):
                    #数据内容相同，对比下一条
                    if zentaoTestData[index] == zentaoSupportData[index]:
                        continue
                    else:
                        # 数据量相等，但数据内容不同 停止同步
                        mylog.log("table data count is equal, but data is not same exit, table:" + table)
                        print(zentaoTestData)
                        print(zentaoSupprotDB)
                        return
            # 禅道测试库数量大于生产库
            if(len(zentaoSupportData) > len(zentaoSupportData)):
                for index in range(0, len(zentaoSupprotDB)):
                    #数据内容相同，对比下一条
                    if zentaoTestData[index] == zentaoSupportData[index]:
                        continue
                    else:
                        # 禅道测试库数据量大于生产库，但生产库部分数据与测试库不同




